import { HttpStatus } from '.'

/**
 * HttpException 是一个继承原生Error错误对象的 内置错误对象
 */
export class HttpException extends Error {
  constructor(public messageOrResponse: string | object, public status: number) {
    super()
  }

  /**
   * 响应JSON
   */
  getResponse() {
    return this.messageOrResponse
  }

  /**
   * 响应状态码
   */
  getStatus() {
    return this.status
  }
}

/**
 * 继承 HttpException 的 BadRequestException 子类
 */
export class BadRequestException extends HttpException {
  constructor(message: string, error?: string) {
    super({ message, error, statusCode: HttpStatus.BAD_REQUEST }, HttpStatus.BAD_REQUEST);
  }
}

/**
 *  继承 HttpException 的 ForbiddenException 子类 用于守卫没有通过时的报错
 */
export class ForbiddenException extends HttpException {
  constructor(message: string, error: string = 'Forbidden') {
      super({ message, error, statusCode: HttpStatus.FORBIDDEN }, HttpStatus.FORBIDDEN);
  }
}
/**
 * 网关错误 502 错误
 */
export class BadGatewayException extends HttpException {
  constructor(message?: string, error: string = 'Bad Gateway') {
      super({ message, error, statusCode: HttpStatus.BAD_GATEWAY }, HttpStatus.BAD_GATEWAY);
  }
}
